package cn.cas.mango.util.paramCheck;

import cn.cas.mango.dto.param.DetailSimParam;
import co.elastic.clients.elasticsearch._types.FieldValue;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class DetailSimParamCheckUtil {
  public static Map<String, Object> checkParam(DetailSimParam formData) {
    Map<String, Object> map = new HashMap<>();
    String isSuccess = ParamCheckUtil.checkParam(formData);
    if (!isSuccess.equals("ok")) {
      map.put("isSuccess", isSuccess);
      return map;
    }
    // id degree alert keywords
    // id 不能为空
    if (Strings.isBlank(formData.getId())) {
      isSuccess = "传入id为空";
      map.put("isSuccess", isSuccess);
      return map;
    }

    // degree相似度阈值 可以为空
    Double degree = formData.getDegree();
    if (degree == null || degree < 0 || degree > 1) {
      formData.setDegree(0.3);
    }
    // alert警戒值 可以为空
    Double alert = formData.getAlert();
    if (alert == null || alert < 0 || alert > 1) {
      formData.setAlert(0.75);
    }

    // keywords可以为空
    if (Strings.isNotBlank(formData.getKeywords())) {
      String[] keywords = formData.getKeywords().trim()
          .replace("inkey:", "").split("\\s+");
      formData.setKeywordsList(new ArrayList<FieldValue>(){{for(String s: keywords) {add(FieldValue.of(s));}}});
    }

    log.info("Request param:{}", formData);
    map.put("isSuccess", isSuccess);
    map.put("formData", formData);
    return map;
  }
}
